package components;

import java.awt.event.ActionListener;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

public abstract class CustomVerifier extends InputVerifier
                                     implements ActionListener {
    
    //Message to be displayed when the data is not valid.
    protected String message;
    //Component to associate verifier to.
    protected JComponent component;

    public CustomVerifier(String message, JComponent component) {
        this.message = message;
        this.component = component;
    }
    
    public boolean isValid() {
        boolean valid = shouldYieldFocus(component);
        
        if (!valid) {
            JOptionPane.showMessageDialog(component.getTopLevelAncestor(),
                    message, "Advertencia", JOptionPane.WARNING_MESSAGE);
            component.requestFocusInWindow();
        }
        
        return valid;
    }
    
    public void forceWrongInput(String message) {
        JOptionPane.showMessageDialog(component.getTopLevelAncestor(),
                    message, "Advertencia", JOptionPane.WARNING_MESSAGE);
            component.requestFocusInWindow();
    }
}
